#include <utils/person.h>


namespace AccountManagerUtils {
	
//! Default constructor
Person::Person() : first_name_(""), last_name_("") {}

//! Copy constructor
Person::Person(const Person& rhs) : first_name_(rhs.first_name_), last_name_(rhs.last_name_) {}

//! Constructor from a first and a last name
Person::Person(const String& first_name, const String& last_name) :
	first_name_(first_name), last_name_(last_name) {}

//! Destructor
Person::~Person() {}

//! Copy constructor
Person& Person::operator=(const Person& rhs) {
	if ( &rhs != this ) {
		first_name_ = rhs.first_name_;
		last_name_ = rhs.last_name_;
	}
	return *this;
}

//! Comparison operator
/*!
 *  This operator only compares the name, but ignores the description
 */
bool Person::operator==(const Person& rhs) const {
	if ( first_name_ != rhs.first_name_ ) return false;
	return ( last_name_ == rhs.last_name_ );
}

//! Difference operator
/*!
 *  This operator only compares the name, but ignores the description
 */
bool Person::operator!=(const Person& rhs) const {
	if ( first_name_ != rhs.first_name_ ) return true;
	return ( last_name_ != rhs.last_name_ );
}

//! Accessor to the first name of the person (read-write)
String& Person::first_name() {
	return first_name_;
}

//! Accessor to the first name of the person (read only)
const String& Person::first_name() const {
	return first_name_;
}

//! Accessor to the middle name of the person (read-write)
String& Person::middle_name() {
	return middle_name_;
}

//! Accessor to the middle name of the person (read only)
const String& Person::middle_name() const {
	return middle_name_;
}

//! Accessor to the last name of the person (read-write)
String& Person::last_name() {
	return last_name_;
}

//! Accessor to the last name of the person (read only)
const String& Person::last_name() const {
	return last_name_;
}

//! Accessor to the maiden name of the person (read-write)
String& Person::maiden_name() {
	return maiden_name_;
}

//! Accessor to the maiden name of the person (read only)
const String& Person::maiden_name() const {
	return maiden_name_;
}

//! Generate the full name of the person (read only)
String Person::full_name() const {
	return first_name_ + " " + middle_name_ + " " + last_name_ + ( maiden_name_.isEmpty() ? "" : ( " (" + maiden_name_ + ")" ) );
}

//===========================================================

//! Initialize the person lib instance
PersonLib* PersonLib::instance_ = NULL;

//! Find a person with given name, if any
const Person* PersonLib::find_person(const String& first_name, const String& last_name) {
	QSet<Person>::iterator it = instance()->persons_.find( Person(first_name,last_name) );
	if ( it == instance()->persons_.end() ) {
		return NULL;
	}
	return &(*it);
}

//! Get the list of persons
const Set<Person>& PersonLib::persons() {
	return instance()->persons_;
}

//! Destructor
PersonLib::~PersonLib() {}

//! Constructor
PersonLib::PersonLib() {}

//! Find or create the instance of PersonLib
PersonLib* PersonLib::instance() {
	if ( instance_ == NULL ) {
		instance_ = new PersonLib();
	}
	return instance_;
}

} // namespace AccountManagerUtils

